// components/calendarDiary/index.js
const api = require("../../config/api")
const request = require("../../utils/request")
const { formatTime, getTwoNaturalWeek, formatNumber
} = require("../../utils/util")
Component({
	properties: {
		hasDayList: Array
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		dateList: [], //日历主体渲染数组
		selectDay: {}, //选中时间
		diaryList: [],
		showTwoWeek: true
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		//上月切换按钮点击
		lastMonth() {
			const { selectDay } = this.data
			const lastMonth = new Date(selectDay.year, selectDay.month - 2)
			const year = lastMonth.getFullYear()
			const month = lastMonth.getMonth() + 1
			this.setMonth(year, month)
		},
		//下月切换按钮点击
		nextMonth() {
			const { selectDay } = this.data
			const nextMonth = new Date(selectDay.year, selectDay.month)
			const year = nextMonth.getFullYear()
			const month = nextMonth.getMonth() + 1
			this.setMonth(year, month)
		},
		//设置月份
		setMonth(setYear, setMonth, setDay) {
			const {
				year,
				month
			} = this.data.selectDay
			if (year !== setYear || month !== setMonth) {
				const data = {
					selectDay: {
						year: setYear,
						month: setMonth,
					},
					showTwoWeek: true
				}
				this.setData(data)
				this.dateInit()
				this.handleChange()
			}
		},
		//根据当前日期获取两个自然周
		getTwoWeek: async function() {
			let { dateList } = this.data
			const {
				year,
				month
			} = this.data.selectDay

			let currentSelectedMonth = formatNumber(month)
			let yearMonth = `${year}-${currentSelectedMonth}`
			let dayList = getTwoNaturalWeek(yearMonth)
			for (const item of dateList) {
				for (const pathItem of dayList) {
					if (pathItem === item.dateString) {
						item.hasDiary = true
					}
				}
			}
			this.setData({
				dateList: dateList
			})
			this.getMonthDiaryList()
		},
		handleWeek(){
			let { dateList, showTwoWeek } = this.data
			dateList.map((item)=>{
				item.hasDiary = false
			})
			if(showTwoWeek){
				this.getAllDay()
			}else{
				this.getTwoWeek()
			}
			this.setData({
				showTwoWeek: !showTwoWeek
			})
		},
		// 整月日期
		getAllDay(){
			let { dateList } = this.data
			dateList.map((item)=>{
				item.hasDiary = false
			})
			const {
				year,
				month
			} = this.data.selectDay
			let now = new Date(year, month - 1) //当前月份的1号
			let startWeek = now.getDay(); //目标月1号对应的星期
			let dayNum = new Date(year, month, 0).getDate() //当前月有多少天
			let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的周数
		
			for (let i = 0; i < forNum; i++) {
				const now2 = new Date(now)
				const day = now2.getDate()
				now2.setDate(i - startWeek + 1)
				let obj = {};
				obj = {
					day: now2.getDate(),
					month: now2.getMonth() + 1,
					year: now2.getFullYear(),
					hasDiary: false,
					isVacant:false,
					isSelect: false,
					dateString: formatTime(now2, "Y-M-D")
				};
				dateList[i] = obj;
			}

			for (const item of dateList) {
				item.hasDiary = true
			}
			this.setData({
				dateList: dateList,
			})
			this.getMonthDiaryList()
		},
		getMonthDiaryList: async function() {
			let { dateList } = this.data
			const { year, month } = this.data.selectDay
			let yearMonth = `${year}-${formatNumber(month)}`
			const patientInfo = wx.getStorageSync('patientInfo')
			const res = await request.get(api.diaryListByDate, {
				patientId: patientInfo.id,
				yearMonth: yearMonth
			},{loading: false})
			if (res.httpStatus === 200) {
				for (const item of dateList) {
					for (const items of res.data) {
						const releaseDay = items.releaseTime.split(' ')[0]
						if (releaseDay === item.dateString) {
							item.isSelect = true
						}
					}
				}
			}
			this.setData({
				dateList: dateList,
			})
		},
		dateInit() {
			const {
				year,
				month
			} = this.data.selectDay

			let dateList = []; //需要遍历的日历数组数据

			let now = new Date(year, month - 1) //当前月份的1号
			let startWeek = now.getDay(); //目标月1号对应的星期
			let dayNum = new Date(year, month, 0).getDate() //当前月有多少天
			let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的周数
		
			for (let i = 0; i < forNum; i++) {
				const now2 = new Date(now)
				now2.setDate(i - startWeek + 1)
				let obj = {};
				obj = {
					day: now2.getDate(),
					month: now2.getMonth() + 1,
					year: now2.getFullYear(),
					hasDiary: false,
					isSelect: false,
					dateString: formatTime(now2, "Y-M-D")
				};
				dateList[i] = obj;
			}
			this.setData({
				dateList: dateList
			})
			this.getTwoWeek()
		},
		handleChange: function(){
			let { year, month } = this.data.selectDay
			setTimeout(()=>{
				let calendarDate = `${year}-${formatNumber(month)}`
				this.triggerEvent("change", calendarDate)
			},100)
		},
	},
	lifetimes: {
		attached() {
			let now = this.data.today ? new Date(this.data.today) : new Date()
			let selectDay = {
				year: now.getFullYear(),
				month: now.getMonth() + 1,
				day: now.getDate(),
				dateString: formatTime(now, "Y-M-D")
			}
			this.setData({
				selectDay,
				chooseDay: selectDay.dateString
			})
			this.dateInit()
		}
	},

})
